package cn.ymxm.utils;

import cn.ymxm.entity.WechatParam;
import cn.ymxm.entity.WechatPush;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.stereotype.Component;

import java.util.Map;

/*
 * @Author Lzh
 * @Date 2022/8/24 21:07
 * @Description
 * @Version 1.0
 **/
@Component
public class Pusher {
    private static String appId;
    private static String secret;
    private static String toUser;
    private static String templateId;
    private static String boyBirthday;
    private static String girlBirthday;
    private static String loveAnniversary;


    public static void wechatParamSetting(WechatParam wechatParam) {
        appId = wechatParam.getAppId();
        secret = wechatParam.getSecret();
        toUser = wechatParam.getToUser();
        templateId = wechatParam.getTemplateId();
        boyBirthday = wechatParam.getBoyBirthday();
        girlBirthday = wechatParam.getGirlBirthday();
        loveAnniversary = wechatParam.getLoveAnniversary();
    }

    public static void push() {
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(appId);
        wxStorage.setSecret(secret);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(toUser)
                .templateId(templateId)
                .build();
        Map<String, String> map = AllUtils.getDailyEnglish();
        WechatPush weather = AllUtils.getWeather();
        WechatPush birthAndLove = AllUtils.getBirthdayAndLove(boyBirthday, girlBirthday, loveAnniversary);
        String title = "L❤M";
        templateMessage.addData(new WxMpTemplateData("title", title, "#FF0000"));
        templateMessage.addData(new WxMpTemplateData("area", weather.getArea() + "", "#C71585"));
        templateMessage.addData(new WxMpTemplateData("weather", weather.getWeather() + "", "#C71585"));
        templateMessage.addData(new WxMpTemplateData("date", weather.getDate() + " " + weather.getWeek(), "#C71585"));
        templateMessage.addData(new WxMpTemplateData("temp", weather.getTemp() + "", "#C71585"));
        templateMessage.addData(new WxMpTemplateData("lowTemp", weather.getLowTemp() + "", "#C71585"));
        templateMessage.addData(new WxMpTemplateData("highTemp", weather.getHighTemp() + "", "#C71585"));
        templateMessage.addData(new WxMpTemplateData("wind", weather.getWind() + "", "#C71585"));
        templateMessage.addData(new WxMpTemplateData("windPower", weather.getWindPower() + "", "#C71585"));
        templateMessage.addData(new WxMpTemplateData("sinceBoyBirthday", birthAndLove.getSinceBoyBirthday() + "", "#C71585"));
        templateMessage.addData(new WxMpTemplateData("sinceGirlBirthday", birthAndLove.getSinceGirlBirthday() + "", "#C71585"));
        templateMessage.addData(new WxMpTemplateData("sinceLoveAnniversary", birthAndLove.getSinceLoveAnniversary() + "", "#C71585"));
        templateMessage.addData(new WxMpTemplateData("goodMorning", AllUtils.getGoodMorning() + "", "#C71585"));
        templateMessage.addData(new WxMpTemplateData("en", map.get("en") + "", "#C71585"));
        templateMessage.addData(new WxMpTemplateData("zh", map.get("zh") + "", "#C71585"));
        try {
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }
    }

}
